package com.zhangyu.shadow.repair.delegate.dept;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.DeptBean;
import com.zhangyu.shadow.repair.delegate.bean.GroupBean;
import com.zhangyu.shadow.repair.delegate.bean.OrgDeptBean;
import com.zhangyu.shadow.repair.delegate.luncher.LauncherDelegate;
import com.zhangyu.shadow.repair.event.LogoutEvent;
import com.zhangyu.shadow.repair.event.OrgResetEvent;
import com.zhangyu.shadow.repair.manager.AccountManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.lib.base.delegate.mvp.MvpDelegate;
import st.lib.base.ui.progress.sweetalert.DialogLoader;
import st.lib.base.ui.progress.sweetalert.IDialogClickListener;
import st.lib.base.util.utilcode.util.ToastUtils;

/**
 * Description:
 *
 * @author Hequn
 * @date 2018/11/5
 */
public class DeptDelegate extends MvpDelegate<DeptPresenter>
        implements DeptContract.View {

    private static final String RESET = "reset";

    public static DeptDelegate newInstance(boolean isReset) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(RESET, isReset);
        DeptDelegate deptDelegate = new DeptDelegate();
        deptDelegate.setArguments(bundle);
        return deptDelegate;
    }

    @BindView(R2.id.sp_dept)
    Spinner mSpDept;
    @BindView(R2.id.sp_group)
    Spinner mSpGroup;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;

    private boolean isReset;
    private OrgDeptBean mCurrentOrgDept;
    private GroupBean mCurrentGroup;
    private DeptBean mOrg;
    private List<GroupBean> mGroupList = new ArrayList<>();
    private DialogLoader mDialogLoader;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isReset = bundle.getBoolean(RESET);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_dept;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setTitle("请选择组织关系");
        if (isReset) {
            mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pop();
                }
            });
        }
        mDialogLoader = new DialogLoader(getContext());
        mPresenter.getOrgDept();
    }

    @Override
    public DeptPresenter getPresenter() {
        return new DeptPresenter(this);
    }

    @Override
    public void showLoading(String str) {
        mDialogLoader.showProgressDialog(true, str, getString(R.string.loading_str_waiting));
    }

    @Override
    public void hideLoading() {
        mDialogLoader.clearDialog();
    }

    @Override
    public void getOrgDeptSucceed(final List<OrgDeptBean> orgDeptBean) {
        try {
            ArrayAdapter<OrgDeptBean> adapterDept = new ArrayAdapter<>(getContext(),
                    R.layout.row_spn, orgDeptBean);
            adapterDept.setDropDownViewResource(R.layout.row_spn_dropdown);
            mSpDept.setAdapter(adapterDept);
            if (!orgDeptBean.isEmpty()) {
                mCurrentOrgDept = orgDeptBean.get(0);
                List<GroupBean> groupBeans = mCurrentOrgDept.getListRepairGroup();
                if (groupBeans != null && !groupBeans.isEmpty()) {
                    mGroupList.addAll(groupBeans);
                    mCurrentGroup = groupBeans.get(0);
                }
            }
            final ArrayAdapter<GroupBean> adapterGroup = new ArrayAdapter<>(getContext(),
                    R.layout.row_spn, mGroupList);
            adapterGroup.setDropDownViewResource(R.layout.row_spn_dropdown);
            mSpDept.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mCurrentOrgDept = orgDeptBean.get(position);
                    mGroupList.clear();
                    if (mCurrentOrgDept.getListRepairGroup() != null) {
                        mGroupList.addAll(mCurrentOrgDept.getListRepairGroup());
                    }
                    adapterGroup.notifyDataSetChanged();
                    if (!mGroupList.isEmpty()) {
                        mSpGroup.setSelection(0);
                        mCurrentGroup = mGroupList.get(0);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            mSpGroup.setAdapter(adapterGroup);
            mSpGroup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    mCurrentGroup = mCurrentOrgDept.getListRepairGroup().get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getOrgDeptFailed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed(msg, null, new IDialogClickListener() {
            @Override
            public void onClick() {
                EventBus.getDefault().post(new LogoutEvent());
            }
        });
    }

    @Override
    public void setPersonOrgDeptSucceed() {
        AccountManager.getInstance().setDeptName(mCurrentOrgDept.getName());
        AccountManager.getInstance().setGroupName(mCurrentGroup.getName());
        AccountManager.getInstance().setGroupId(mCurrentGroup.getId());
        AccountManager.getInstance().setDepId(mCurrentOrgDept.getId());
        start(LauncherDelegate.newInstance(), SINGLETASK);
        EventBus.getDefault().post(new OrgResetEvent());
    }

    @Override
    public void setPersonOrgDeptFailed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed(msg, null, new IDialogClickListener() {
            @Override
            public void onClick() {
                EventBus.getDefault().post(new LogoutEvent());
            }
        });
    }

    @OnClick(R2.id.btn_save)
    public void onSave() {
        if (mCurrentOrgDept == null) {
            ToastUtils.showShortSafe("请选择维修厂");
            return;
        }
        if (mCurrentGroup == null) {
            ToastUtils.showShortSafe("请选择维修组");
            return;
        }
        mPresenter.setPersonOrgDept(AccountManager.getInstance().getWorkId(),
                mCurrentOrgDept.getId(),
                mCurrentGroup.getId());
    }
}
